package com.back.end.utils.timer;

import com.back.end.Dao.OrderDao;
import com.back.end.Modal.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

@Component
public class OrderTimer {

    @Resource
    OrderDao orderDao;

    @Scheduled(cron = "0 0 0 * * ? ")

    //循环检查订单是否过期
    public void OrderCheck() {
        List<Order> orders = orderDao.getAllOrderList();
        Timestamp now = new Timestamp(System.currentTimeMillis());
        orders.forEach(item -> {
            long apartTime = now.getTime() - item.getCreatedTime().getTime();
            if (apartTime / 1000 / 3600 >= 24 && item.getOrderStatus().equals("UNFIN")) {
                orderDao.finishOrder(item.getId(), now);
            }
            if (apartTime / 1000 / 3600 / 24 >= 7 &&
                    (item.getOrderStatus().equals("FIN") || item.getOrderStatus().equals("REFUNDED"))) {
                orderDao.deleteFinishedOrder(item.getId());
            }
        });

    }
}
